import routerConfig from '../../config/router'

export default {
  namespace:'nav',

  state:{
    selectedNavs:['/submit']
  },

  effects: {
    *changNav({ payload }, { put }) {
      yield put({
        type: 'nav',
        payload: payload,
      });
    },
  },

  reducers:{
    nav(state, action) {
      return {
        ...state,
        selectedNavs: [action.payload.path]
      };
    },
  },

  subscriptions: {
    setupHistory({ dispatch, history }) {

      history.listen(location => {
        let routes = routerConfig[0].routes;
        routes.map(item=>{
          if(location.pathname === item.path && location.pathname !=='/'){
            dispatch({
              type: 'nav/changNav',
              payload:{
                path: item.path
              }
            })
          }
        })
      });
    },
  },

}
